package com.sunshine.security;


import com.sunshine.bean.User;
import com.sunshine.constant.UserRole;
import com.sunshine.exception.UnknownUserRoleException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;

import static com.sunshine.constant.UserRole.ADMIN_USER;
import static com.sunshine.constant.UserRole.COMM_USER;
import static java.util.Arrays.asList;

/**
 * Created by along on 4/19/18.
 */
public class JwtUser implements UserDetails {

    private int id;


    private String name;

    private String nickname;

    private String password;

    private Timestamp createdAt;

    private String currentToken;

    private Collection<? extends GrantedAuthority> authorities;


    public JwtUser(int id, String name, String nickname, String password, Timestamp createdAt, Collection<? extends GrantedAuthority> authorities) {
        this.id = id;
        this.name = name;
        this.nickname = nickname;
        this.password = password;
        this.createdAt = createdAt;
        this.authorities = authorities;
    }

    public static JwtUser of(User user) throws UnknownUserRoleException {
        if (user == null) {
            throw new NullPointerException("user is null");
        }

        return new JwtUser(user.getId(),
                user.getName(),
                user.getNickname(),
                user.getPassword(),
                user.getCreatedAt(),
                mapToGrantedAuthorities(user.getRole()));
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(String roles) throws UnknownUserRoleException {
        String role;
        switch (roles) {
            case COMM_USER:
                role = UserRole.ROLE_COMM;
                break;
            case ADMIN_USER:
                role = UserRole.ROLE_ADMIN;
                break;
            default:
                throw new UnknownUserRoleException("unknown role : " + roles);
        }

        return asList(new SimpleGrantedAuthority(role));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public int getId() {
        return id;
    }

    public Timestamp getCreatedAt() {
        return createdAt;
    }

    @Override
    public String getPassword() {
        return password;
    }

    /**
     * Notice:
     * user name is id
     *
     * @return id
     */
    @Override
    public String getUsername() {
        return String.format("%d", id);
    }

    public String getName() {
        return name;
    }

    public String getNickname() {
        return nickname;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public String getCurrentToken() {
        return currentToken;
    }

    public void setCurrentToken(String currentToken) {
        this.currentToken = currentToken;
    }
}